package org.ucas.kgraph.entity;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.google.common.collect.Lists;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * 角色类,帮助进行权限控制
 *
 * @author suemi
 */
@Entity
@Table
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends IdEntity{
	@Column
	private String name;
	
	
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="ss_role_perm",joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = { @JoinColumn(name = "perm_id") })
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy("id ASC")
	@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private List<Perm> permList= Lists.newArrayList();
	 
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public List<Perm> getPermList() {
		return permList;
	}
	
	public void setPermList(List<Perm> permList) {
		this.permList = permList;
	}
	@Transient
	public Set<String> getPermsName() {
	    List<Perm> perms = getPermList();
	    Set<String> set = new HashSet<String>();
	    for (Perm perm : perms) {
	        set.add(perm.getName());
	    }
	    return set;
	}
}
